home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc51bugs.zip / Q37795 < prev    next >
Text File  |  1988-11-15  |  1KB  |  65 lines

  1. Q37795 "if" Evaluates Incorrectly Using Unsigned Arithmetic
  2. C Compiler
  3. 5.10
  4. MS-DOS
  5.  
  6. Summary:
  7.  
  8. When an expression involving unsigned arithmetic is placed inside an
  9. "if" statement, the expression may fail to evaluate correctly.
  10.  
  11. Microsoft has confirmed this to be a problem in Version 5.10. We are
  12. researching this problem and will post new information as it becomes
  13. available.
  14.  
  15. More Information:
  16.  
  17. The following statements illustrate the problem:
  18.  
  19.  #include<stdio.h>
  20.  unsigned int a;
  21.  
  22.  main()
  23.  {
  24.     int b,c;
  25.     a=20;
  26.     b=1;
  27.     c=-1;
  28.     a+=;
  29.     if((a + (b * c) >= 0)
  30.  
  31. /* This expression evaluates to false, although the arithmetic
  32.  expression should evaluate to 19.*/
  33.     {
  34.       puts("this should have worked");
  35.     }
  36.  }
  37.  
  38. If the following change is made in the above code fragment, the
  39. expression will evaluate correctly:
  40.  
  41.  unsigned int a;
  42.           int b, c, d;
  43.  
  44.  .
  45. .
  46.  .
  47.  
  48.  d = (a + (b * c));
  49.  if ( d >= 0);
  50.  
  51. The following are other possible workarounds:
  52.  
  53. 1. Casting the expression to integer arithmetic, as follows:
  54.  
  55.       if ( ((int)(a + (b * c))) >= 0 )
  56.  
  57. 2. Change the declaration of the unsigned value, as follows:
  58.  
  59.       int a;
  60.     instead of:
  61.       unsigned int a;
  62.  
  63. Keywords:  buglist5.10
  64. Updated  88/11/15 06:14
  65.